<?php
class Locations extends Controller
{
	private $tbl = 'tbl_locations';
 	function Locations()
	{
		parent::Controller();
                $this->load->model('locations_model');
	}
	
	function index()
	{
		$data['total_locations'] = $this->locations_model->total_locations();
                create_pagination($data['total_locations'], 3, config_item('per_page'));
                $data['locations'] = $this->locations_model->get_locations(config_item('per_page'), $this->uri->segment(3));
		//$data['locations']	= $this->general_db_model->getAll($this->tbl, 'location_name');
               
                //debug_array($data['locations']);exit;
		$data['page_title']	= 'Manage Locations';
		$data['page'] = 'locations_list_v';
		$this->load->view('main', $data);	
	}
	
	function add()
	{
		if($this->input->post('submit_location'))
		{
			$this->add_edit();	
		}
		$data['page_title']	 = 'Add New Locations';
		$data['page'] = 'locations_add_v';
		$this->load->view('main', $data);
	}
	
	function edit($id)
	{
		if($this->input->post('submit_location'))
		{
			$this->add_edit($id);	
		}
		//$data['categories'] = $this->general_db_model->getAll($this->tbl, 'locations_name', 'parent_id = 0');
		$data['_location'] = $this->general_db_model->getById($this->tbl, 'id', $id);
		$data['page_title']	 = 'Update Locations';
		$data['page'] = 'locations_add_v';
		$this->load->view('main', $data);	
	}
	
	function add_edit($id = NULL)
	{
		 $data['location_name'] = $this->input->post('location_name');
		 if($id)
		 {
		 	$this->general_db_model->update($this->tbl, $data, 'id = '.$id);
			$this->session->set_flashdata('success_message', "Location - <strong>".$data['location_name']." </strong>Successfully updated.");
		 }
		 else
		 {
		 	$this->general_db_model->insert($this->tbl, $data);
		 	$this->session->set_flashdata('success_message', "Location - <strong>".$data['location_name']." </strong>Successfully added.");
		 }
		 
		 redirect(current_url());
	}
	
	function delete()
	{
	 	$location_id = $this->input->post('c_id');
		$this->general_db_model->delete($this->tbl, 'id = '.$location_id);
	}
}

?>